<?php namespace system\model;
use houdunwang\model\Model;
class Article extends Model{
	//数据表
	protected $table = "article";

	//允许填充字段
	protected $allowFill = [ ];

	//禁止填充字段
	protected $denyFill = [ ];

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]
		//title、sendtime、thumb、digest、author、
		//keywords、user_uid、category_cid
		['title','isnull','请填写标题',self::MUST_VALIDATE,self::MODEL_BOTH],
		['thumb','isnull','请上传缩略图',self::MUST_VALIDATE,self::MODEL_BOTH],
		['digest','isnull','请填写文章摘要',self::MUST_VALIDATE,self::MODEL_BOTH],
		['author','isnull','请填写文章作者',self::MUST_VALIDATE,self::MODEL_BOTH],
		['keywords','isnull','请填写关键字',self::MUST_VALIDATE,self::MODEL_BOTH],
		['category_cid','isnull','请选择文章分类',self::MUST_VALIDATE,self::MODEL_BOTH],
	];

	//自动完成
	protected $auto=[
		//['字段名','处理方法','方法类型',验证条件,验证时机]
		['sendtime','time','function',self::MUST_AUTO,self::MODEL_INSERT],
		['updatetime','time','function',self::MUST_AUTO,self::MODEL_UPDATE],
		//getUid使我们自定义的方法，在下面
		['user_uid','getUid','method',self::MUST_AUTO,self::MODEL_BOTH],
	];
	public function getUid($val,$data)
	{
		//return md5($val);
		return Session::get('uid');
	}
	/**
	 * 获取所有分类数据
	 * @return mixed   树状结构的分类数据
	 */
	public function getCateData()
	{
		//Arr:tree变成树状结构
		return Arr::tree(Db::table('category')->get(),'cname');
	}

	/**
	 * 获取所有标签数据
	 * @return mixed   标签数据
	 */
	public function getTagData()
	{
		//获取标签表中所有数据
		return Db::table('tag')->get();
	}
	public function getAllData($num,$is_recycle)
	{
		//分页处理、所有数据
		//pageNum设置显示的页码数量
		//row每页显示条数
		//echo Page::pageNum(5)->row(5)->make(100);
		//echo Page::desc(['pre'=>'上楼', 'next'=>'下楼','first'=>'首页','end'=>'尾页','unit'=>'个'])->make(100,5);
		//echo Page::limit();

		//统计文章表不在回收站的数据总数
		$count = Db::table('article')->where('is_recycle',$is_recycle)->count();
		//p($count);
		$page = Page::row($num)->make($count);
		Page::desc(['pre'=>'上楼', 'next'=>'下楼','first'=>'首页','end'=>'尾页','unit'=>'个'])->make($count,$num);
		//数据
		//$data = Db::table('article')->where('is_recycle',0)->limit(Page::limit())->get();
		$data = Db::table('article')
			->join('category','article.category_cid','=','category.cid')
			->where('is_recycle',$is_recycle)->limit(Page::limit())
			->get();
		//p($data);
		return ['page'=>$page,'data'=>$data];
	}
	/**
	 * 添加
	 */
	public function store()
	{
		if(!isset($_POST['tag'])){
			return ['valid'=>0,'message'=>'请选择标签'];
		}
		//1.添加文章表【两部分：下面第一部分+自动完成】
		//title、sendtime、thumb、digest、author、
		//keywords、user_uid、category_cid
		$this->title = $_POST['title'];
		$this->author = $_POST['author'];
		$this->category_cid = $_POST['category_cid'];
		$this->thumb = $_POST['thumb'];
		$this->digest = $_POST['digest'];
		$this->keywords = $_POST['keywords'];
		//p($_POST);die;
		$aid = $this->save();
		//p($aid);die;


		//2.添加文章数据表
		//keywords在文章表处理过了，这里不处理他了
		//des、content、article_aid
		$artitleDataModel = new ArticleData();
		$artitleDataModel->des = $_POST['des'];
		$artitleDataModel->content = $_POST['content'];
		$artitleDataModel->article_aid = $aid;
		$artitleDataModel->save();

		//3.添加文章标签中间表
		//article_aid   tag_tid
		//1    10
		//1    11
		//1    12
		$articleTagModel = new ArticleTag();
		//做判断，验证文章标签必须得选择,判断放在最上面
		foreach($_POST['tag'] as $v)
		{
			$articleTagModel->article_aid = $aid;
			$articleTagModel->tag_tid = $v;
			$articleTagModel->save();
		}
		return ['valid'=>1,'message'=>'操作成功'];
	}
	/**
	 * 获取要编辑的文章的旧数据
	 */
	public function getOldData($aid)
	{
		return Db::table('article')
		->join('article_data','article.aid','=','article_data.article_aid')
		->where('article.is_recycle',0)->where('article.aid','=',$aid)->first();
	}
	/**
	 * 编辑文章
	 */
	public function edit()
	{
		if(!isset($_POST['tag'])){
			return ['valid'=>0,'message'=>'请选择标签'];
		}
		$aid = $_POST['aid'];
		//1.文章表的修改
		$articleModel = $this->find($aid);
		$articleModel->title = $_POST['title'];
		$articleModel->author = $_POST['author'];
		$articleModel->category_cid = $_POST['category_cid'];
		$articleModel->thumb = $_POST['thumb'];
		$articleModel->digest = $_POST['digest'];
		$articleModel->keywords = $_POST['keywords'];
		//p($_POST);die;
		$articleModel->save();


		//2.文章数据表的修改

			//根据要编辑的文章的id获取违章数据表对应的主键ad_id
			$ad_id = Db::table('article_data')->where("article_aid",$aid)->pluck('ad_id');
			//echo $ad_id;die;
			$articleDataModel = ArticleData::find($ad_id);
			$articleDataModel->des = $_POST['des'];
			$articleDataModel->content = $_POST['content'];
			$articleDataModel->article_aid = $aid;
			$articleDataModel->save();
		//文章标签中间表的修改【删除原来数据+新增】
			$articleTagModel = new ArticleTag();
			//1.在文章标签中间表中，删除原来当前文章的相关数据
				$articleTagModel->where('article_aid',$aid)->delete();
			//2.新增
			//做判断，验证文章标签必须得选择,判断放在最上面
			foreach($_POST['tag'] as $v)
			{
				$articleTagModel->article_aid = $aid;
				$articleTagModel->tag_tid = $v;
				$articleTagModel->save();
			}
		return ['valid'=>1,'message'=>'操作成功'];
	}
	/**
	 * 回收真正的删除
	 */
	public function del($aid)
	{
		//1.删除文章表
		$this->where('aid',$aid)->delete();
		//2.删除文章数据表
		(new ArticleData())->where('article_aid',$aid)->delete();
		//3.删除文章标签中间表
		(new ArticleTag())->where('article_aid',$aid)->delete();
		return true;
	}
}